package com.traffic.between.adaptor;

import java.util.Date;
import org.apache.log4j.Logger;
import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.ShenzltServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;


@ChooseProvider(channel = "szlt")
public class ShenzltAdaptor implements IAdaptor{

	private static Logger log = Logger.getLogger(ShenzltAdaptor.class);

	public void order() {
		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());
		
		
		String productId = orderBean.getProductCode();
		String phoneNo = orderBean.getPhoneNo();
		String cstmOrderNo = orderBean.getIds();
		log.info("bsm- 深圳联通订单ShenzltService.order() 订购请求productId:" + productId+",phoneNo:"+phoneNo+",cstmOrderNo:"+cstmOrderNo);
		String result = ShenzltServiceImpl.service.order(productId, phoneNo, cstmOrderNo);
		JSONObject json = JSONObject.fromObject(result);

		String code = (String) json.get("resultCode");
		
		// 00表示订购中
		if (("00".equals(code))) {
			orderBean.setMessage((String) json.get("resultDesc"));
			orderBean.setErrorMsg("订购中");
			orderBean.setStatus("11");
			orderBean.setSuppOrderNo(String.valueOf(json.get("orderId")));
		} else {

			orderBean.setMessage((String) json.get("resultDesc"));
			orderBean.setErrorMsg("订购失败");
			orderBean.setStatus("8");
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	
	public void status() {
		OrderBean orderBean = OrderInfoContext.getContext();
		

		String cstmOrderNo = orderBean.getIds();
		log.info("bsm -深圳联通状态ShenzltService.orderStatus() 查询请求cstmOrderNo :" + cstmOrderNo);
		String result = ShenzltServiceImpl.service.orderStatus(cstmOrderNo);

		JSONObject json = JSONObject.fromObject(result);

		String code = (String) json.get("resultCode");

		if (("00".equals(code))) {
		
			orderBean.setMessage((String) json.get("resultMsg"));
			orderBean.setErrorMsg("订购成功");
			orderBean.setStatus("7");
		} else {
			
			orderBean.setMessage((String) json.get("resultDesc"));
			orderBean.setErrorMsg((String) json.get("resultDesc"));
			orderBean.setStatus("8");
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}
}
